#include "../include/CacheManager.h"
#include "../include/LRUCache.h"
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

void test()
{
    CacheManager::getInstance();

    //打印0~3号缓存的resultList
    cout << endl << "test1" << endl;
     for (int i = 0; i < 4;++i){
        cout << i << "号缓存：" << endl;
        auto cache = CacheManager::getInstance()->getCache(i);
        cache.printResultList();
     }

    //向0~3号缓存中写入数据
    cout << endl << "test2" << endl;
    string cache0K = "0号key";
    string cache0V = "你好 再见 好人";   
    string cache0K1 = "1号key";
    string cache0V1 = "舔狗";

    //在0号线程对应的缓存中查找关键字cache0K
    string ret = CacheManager::getInstance()->getCache(0).SearchKeyword(cache0K);
    if(ret.empty()){
        //缓存中找不到，插入key，value
        CacheManager::getInstance()->getCache(0).addPendingList(cache0K, cache0V);
    }
    else{
        //如果找到了，ret 是value
    }
    string ret1 = CacheManager::getInstance()->getCache(0).SearchKeyword(cache0K1);
    if(ret1.empty()){
        //缓存中找不到，插入key，value
        CacheManager::getInstance()->getCache(0).addPendingList(cache0K1, cache0V1);
    }
    cout << "printPendingList:" << endl;
    CacheManager::getInstance()->getCache(0).printPendingList();



    //再次查阅
    cout << endl << "test3" << endl;
    //更新
    CacheManager::getInstance()->periodicUpdateCaches();
    cout << "printPendingList:" << endl;
    //CacheManager::getInstance()->getCache(0).printPendingList();

    CacheManager::getInstance()->getCache(0).printResultList();
    string ret2 = CacheManager::getInstance()->getCache(0).SearchKeyword("b");
    if(ret2.empty()){
        //缓存中找不到，插入key，value
        cout << "找不到" << endl;
        CacheManager::getInstance()->getCache(0).addPendingList("b", "bbbbb");
    }
    else
    {
        cout << "找到了" << endl;
    }
    cout << "ret2 = " << ret2 << endl;
    cout << "printResultList:" << endl;
    CacheManager::getInstance()->getCache(0).printResultList();

    CacheManager::getInstance()->periodicUpdateCaches();
    cout << "printPendingList:" << endl;
    CacheManager::getInstance()->getCache(0).printPendingList(); 

}


void test1()
{
    CacheManager::getInstance();

    //打印0~3号缓存的resultList
    cout << endl << "test1" << endl;
    for (int i = 0; i < 4;++i){
       cout << i << "号缓存：" << endl;
       auto cache = CacheManager::getInstance()->getCache(i);
       cache.printResultList();
    }

    string ret2 = CacheManager::getInstance()->getCache(0).SearchKeyword("b");
    if(ret2.empty()){
        //缓存中找不到，插入key，value
        cout << "找不到" << endl;
        CacheManager::getInstance()->getCache(0).addPendingList("b", "bbbbb");
    }
    else
    {
        cout << "找到了" << endl;
    }
    cout << "ret2 = " << ret2 << endl;
    cout << "printResultList:" << endl;
    CacheManager::getInstance()->getCache(0).printResultList();

}

void test2()
{
    CacheManager::getInstance();

    //打印0~3号缓存的resultList
    cout << endl << "test1" << endl;
    for (int i = 0; i < 4;++i){
       cout << i << "号缓存：" << endl;
       auto cache = CacheManager::getInstance()->getCache(i);
       cache.printResultList();
    }

    string ret = CacheManager::getInstance()->getCache(0).SearchKeyword("g");
    if(ret.empty()){
        //缓存中找不到，插入key，value
        cout << "找不到" << endl;
        CacheManager::getInstance()->getCache(0).addPendingList("g", "ggggg");
    }

    string ret1 = CacheManager::getInstance()->getCache(0).SearchKeyword("h");
    if(ret1.empty()){
        //缓存中找不到，插入key，value
        cout << "找不到" << endl;
        CacheManager::getInstance()->getCache(0).addPendingList("h", "hhhhh");
    }

    CacheManager::getInstance()->getCache(0).printPendingList();

    string ret7 = CacheManager::getInstance()->getCache(0).SearchKeyword("g");
    if(ret7.empty()){
        //缓存中找不到，插入key，value
        cout << "找不到" << endl;
        CacheManager::getInstance()->getCache(0).addPendingList("g", "ggggg");
    }
    
    CacheManager::getInstance()->getCache(0).printPendingList();

    string ret2 = CacheManager::getInstance()->getCache(1).SearchKeyword("i");
    if(ret2.empty()){
        //缓存中找不到，插入key，value
        cout << "找不到" << endl;
        CacheManager::getInstance()->getCache(1).addPendingList("i", "iiiiii");
    }

    string ret3 = CacheManager::getInstance()->getCache(2).SearchKeyword("j");
    if(ret3.empty()){
        //缓存中找不到，插入key，value
        cout << "找不到" << endl;
        CacheManager::getInstance()->getCache(2).addPendingList("j", "jjjjj");
    }

    string ret4 = CacheManager::getInstance()->getCache(3).SearchKeyword("k");
    if(ret4.empty()){
        //缓存中找不到，插入key，value
        cout << "找不到" << endl;
        CacheManager::getInstance()->getCache(3).addPendingList("k", "kkkkk");
    }

    CacheManager::getInstance()->periodicUpdateCaches();
    cout << "printResultList:" << endl;
    CacheManager::getInstance()->getCache(0).printResultList();
}

int main()
{
    test2();
    return 0;
}
